import java.util.Stack;

//单向链表
public class MyStringleList {
    //内部类
    static class Node {
        public int val;//存储的数据
        public Node next;//存储下一个节点的地址

        //构造方法
        public Node(int val) {
            this.val = val;
        }
    }

    public Node head;// 代表头结点的引用

    public void createLink() {
        Node node1 = new Node(12);
        Node node2 = new Node(111);
        Node node3 = new Node(33);
        Node node4 = new Node(78);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        head = node1;
    }

    /**
     * 遍历打印链表
     */
    public void display() {
        Node node = head;
        while (node != null) {
            System.out.print(node.val + " ");
            node = node.next;
        }
        System.out.println();
    }

    public void display(Node newHead) {
        while (newHead != null) {
            System.out.print(newHead.val + " ");
            newHead = newHead.next;
        }
        System.out.println();
    }

    //递归反向打印
    public void display1(Node pHead) {
        if (pHead == null) {
            return;
        }
        display1(pHead.next);
        System.out.print(pHead.val + " ");
    }

    //利用栈反向打印
    public void display2(Node pHead) {
        if (pHead == null) {
            return;
        }
        //创建一个栈将结点放入栈
        Stack<Node> stack = new Stack<>();
        Node cur = head;
        while (cur != null) {
            stack.push(cur);
            cur = cur.next;
        }
        //出栈
        while(!stack.empty()) {
            System.out.print(stack.pop().val + " ");
        }
        System.out.println();
    }


    //查找关键字key是否在单链表当中
    public boolean contains(int key) {
        Node cur = head;
        while (cur != null) {
            //如果是引用类型要用equals
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //得到链表的长度
    public int size() {
        int count = 0;
        Node cur = head;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    //头插法
    public void addFirst(int val) {
        Node node = new Node(val);
        node.next = head;
        head = node;
    }

    //尾插法
    public void addLast(int val) {
        Node node = new Node(val);
        if (head == null) {
            head = node;
            return;
        }
        Node cur = head;
        while (cur.next != null) {
            cur = cur.next;
        }
        cur.next = node;
    }

    //任意位置插入，头节点下标为0
    //遍历到要插入位置的前一个
    //因为不能向前访问
    //下标合法性判断
    public void addindex(int val, int index) throws IndexOutOfException{
        checkIndex(index);
        if (index == 0) {
            addFirst(val);
            return;
        }
        if (index == size()) {
            addLast(val);
            return;
        }

        Node cur = head;
        for (int i = 0; i < index - 1; i++) {
            cur = cur.next;
        }
        Node node = new Node(val);
        node.next = cur.next;
        cur.next = node;
    }

    //
    private void checkIndex(int index) throws IndexOutOfException{
        if (index < 0 || index > size()) {
            throw new IndexOutOfException("位置不合法");
        }
    }

    //删除第一次出现关键字key的节点
    public void remove(int val) {
        if (head == null) {
            return;
        }
        if(head.val == val) {
            head = head.next;
            return;
        }
        Node cur = searchPrev(val);
        if (cur != null) {
            cur.next = cur.next.next;
        }
    }

    //找到删除节点的前驱
    private Node searchPrev(int val) {
        Node cur = head;
        while (cur.next != null) {
            if (cur.next.val == val) {
                return cur;
            }
            cur = cur.next;
        }
        return null;
    }

    //删除所有关键字key的节点
    public void removeAllKey1(int val) {
        if (head == null) {
            return;
        }

        Node cur = head;
        while(cur.next != null) {
            if(cur.next.val == val) {
                cur.next = cur.next.next;
                continue;
            }
            cur = cur.next;
        }

        //判断第一个节点
        if (head.val == val) {
            head = head.next;
        }
    }

    public void removeAllKey(int val) {
        if (head == null) {
            return;
        }

        Node prev = head;
        Node cur = head.next;
        while (cur != null) {
            if (cur.val == val) {
                prev.next = cur.next;
                cur = cur.next;
            } else {
                prev = cur;
                cur = cur.next;
            }
        }

        if (head.val == val) {
            head = head.next;
        }
    }

    //清空链表
    public void clear() {
        head = null;
    }


    //反转链表
    public Node reverseList() {
        if (head == null || head.next == null) {
            return head;
        }
        Node cur = head.next;
        head.next = null;
        while (cur != null) {
            Node curNext = cur.next;
            cur.next = head;
            head = cur;
            cur = curNext;
        }
        return head;
    }

    //返回中间结点
    public Node middleNode() {
        //快慢指针
        //fast的速度是slow的2倍
        //fast到终点时 slow就是中间结点
        Node fast = head;
        Node slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }
        return slow;
    }
}